package com.controller;

//import ch.qos.logback.core.model.Model;
import com.entity.BGoods;
import com.entity.BType;
import com.entity.table.BGoodsTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.impl.BGoodsServiceImpl;
import com.service.impl.BTypeServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Controller
public class IndexController {
    @Resource
    BGoodsServiceImpl bGoodsService;
    @Resource
    BTypeServiceImpl bTypeService;
    @GetMapping(value ="")
    public String index(Model model){
        try{
            Map<String, List<BGoods>> goodsList= new LinkedHashMap<>();
            //查询商品分类
            QueryWrapper wrapper= QueryWrapper. create().limit(6);

            List<BType> typeList= bTypeService. getMapper(). selectListByQuery(wrapper);
            for (BType bType :typeList){
                int typeId =bType.getId();
                String typeName =bType.getTypeName();
                //查询商品列表
                wrapper =QueryWrapper.create(). where(BGoodsTableDef.B_GOODS.TYPE_ID.eq(typeId)).limit(4);
                List<BGoods> goods= bGoodsService.list(wrapper);
                goodsList.put(typeName, goods);
            }
            model.addAttribute( "goodType", typeList);
            model.addAttribute("goodsList",goodsList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "client/index";
    }

    @GetMapping(value = "/list")
    public String goodList(ModelMap model, @RequestParam(value = "goodName", defaultValue = "") String goodName,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        try {
            List<BType> typeList=bTypeService.list();
            QueryWrapper queryWrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
            Page<BGoods> page = new Page<>();
            page.setPageSize(pageSize);
            page.setPageNumber(p);
            Page<BGoods> pageInfo = bGoodsService.getMapper().paginateWithRelations(page, queryWrapper);
            model.addAttribute("page", pageInfo);
            model.addAttribute("total", pageInfo.getTotalRow());
            model.addAttribute("goodType", typeList);
            return "client/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "client/login";
        }
    }  @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model){
        BType bType = bTypeService.getById(id);
        model.addAttribute("type",bType);
        return "back/type-edit";
    }
    @GetMapping(value = "goodDetail")
    public String goodDetail(int id, ModelMap model) {
        try {
            List<BType> typeList=bTypeService.list();
            BGoods bGoods = bGoodsService.getById(id);

            model.addAttribute("goodType", typeList);
            model.addAttribute("bGood", bGoods);
            return "client/detail";
        }catch (Exception e){
            e.printStackTrace();
            return "client/login";
        }
    }
}
